package www.dianbaozu.com.ui.chat.friendRequest.adapter

import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.module.LoadMoreModule
import com.netease.nim.uikit.api.NIMHelper
import com.netease.nim.uikit.api.NIMRepository
import com.netease.nim.uikit.business.uinfo.UserInfoHelper
import com.netease.nim.uikit.common.ui.recyclerview.adapter.BaseQuickAdapter
import com.netease.nim.uikit.common.ui.recyclerview.holder.BaseViewHolder
import com.netease.nim.uikit.databinding.ItemFriendRequestBinding
import com.netease.nimlib.sdk.RequestCallback
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum
import com.netease.nimlib.sdk.msg.model.SystemMessage
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo

class FriendRequestAdapter(
    recyclerView: RecyclerView,
    layoutId: Int,
    data: List<SystemMessage>,
    private val onItemClick: OnItemClick
) :
    BaseQuickAdapter<SystemMessage, BaseViewHolder>(recyclerView, layoutId, data) {
    private var binding: ItemFriendRequestBinding? = null

    override fun convert(
        helper: BaseViewHolder,
        item: SystemMessage,
        position: Int,
        isScrolling: Boolean
    ) {
        binding = DataBindingUtil.bind(helper.itemView)
        binding?.apply {
            ivAvatar.loadBuddyAvatar(item.fromAccount)
            tvNickname.text = UserInfoHelper.getUserTitleName(item.fromAccount, SessionTypeEnum.P2P)
            NIMHelper.fetchUserInfo(mutableListOf(item.fromAccount),object :RequestCallback<List<NimUserInfo>>{
                override fun onSuccess(param: List<NimUserInfo>?) {
                    tvNickname.text = param?.get(0)?.name
                }

                override fun onFailed(code: Int) {
                }

                override fun onException(exception: Throwable?) {
                }
            })
            ivAccept.setOnClickListener { onItemClick.onAccept(helper.layoutPosition, item) }
            ivRefuse.setOnClickListener { onItemClick.onRefuse(helper.layoutPosition, item) }
        }
    }

    interface OnItemClick {
        fun onAccept(position: Int, item: SystemMessage)
        fun onRefuse(position: Int, item: SystemMessage)
    }
}